package org.szgb.console.sys.line.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysLineLocation extends BeanEntity<SysLineLocation> {
    @Serial
    private static final long serialVersionUID = 1L;
    private SysLine line;
    private Integer type;
    private String name;

    public SysLineLocation() {
    }

    public SysLineLocation(String id) {
        this.setId(id);
    }

    public SysLineLocation(SysLineLocation.SysLineLocationFindDto sysLineLocationFindDto) {
        this.setId(sysLineLocationFindDto.getId());
        this.setLine(new SysLine(sysLineLocationFindDto.getLineId()));
        this.setType(sysLineLocationFindDto.getType());
        this.setName(sysLineLocationFindDto.getName());
        this.setPageDto(sysLineLocationFindDto.getPageDto());
    }

    public SysLineLocation(SysLineLocation.SysLineLocationSaveDto sysLineSaveDto) {
        this.setId(sysLineSaveDto.getId());
        this.setName(sysLineSaveDto.getName());
        this.setLine(new SysLine(sysLineSaveDto.getLineId()));
        this.setType(sysLineSaveDto.getType());
    }

    public SysLineLocationView toSysLineLocationView() {
        return new SysLineLocation.SysLineLocationView(this);
    }


    @Data
    @Schema(title = "线路站点信息", description = "线路站点信息")
    public static class SysLineLocationView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "线路id")
        private String lineId;
        @Schema(description = "线路名称")
        private String lineName;
        @Schema(description = "站点类型")
        private Integer type;
        @Schema(description = "名称")
        private String name;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysLineLocationView(SysLineLocation sysLineLocation) {
            this.id = sysLineLocation.getId();
            this.lineId = sysLineLocation.getLine().getId();
            this.lineName = sysLineLocation.getLine().getName();
            this.type = sysLineLocation.getType();
            this.name = sysLineLocation.getName();
            this.createdAt = sysLineLocation.getCreatedAt();
            this.updatedAt = sysLineLocation.getUpdatedAt();
            if (sysLineLocation.getCreatedBy() != null) {
                this.createdById = sysLineLocation.getCreatedBy().getId();
                this.createdByName = sysLineLocation.getCreatedBy().getName();
            }
            if (sysLineLocation.getUpdatedBy() != null) {
                this.updatedById = sysLineLocation.getUpdatedBy().getId();
                this.updatedByName = sysLineLocation.getUpdatedBy().getName();
            }
        }

        public static List<SysLineLocation.SysLineLocationView> getViewListFromList(List<SysLineLocation> SysLineLocationList) {
            return SysLineLocationList.stream().map(SysLineLocation.SysLineLocationView::new).toList();
        }
    }

    @Data
    @Schema(title = "线路站点树表信息", description = "线路站点树表信息")
    public static class SysLineLocationTreeView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "系统树")
        List<SysLineLocationSystem.SysLineLocationSystemTreeView> systemTree;

        public SysLineLocationTreeView(SysLineLocation sysLineLocation, List<SysLineLocationSystem.SysLineLocationSystemTreeView> systemTree) {
            this.id = sysLineLocation.getId();
            this.name = sysLineLocation.getName();
            this.systemTree = systemTree;
        }
    }

    @Data
    @Schema(title = "线路站点树表子项信息", description = "线路站点树表子项信息")
    public static class SysLineLocationTreeItemView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;

        public SysLineLocationTreeItemView(SysLineLocation sysLineLocation) {
            this.id = sysLineLocation.getId();
            this.name = sysLineLocation.getName();
        }

        public static List<SysLineLocation.SysLineLocationTreeItemView> getViewListFromList(List<SysLineLocation> sysLineLocationList) {
            return sysLineLocationList.stream().map(SysLineLocation.SysLineLocationTreeItemView::new).toList();
        }
    }

    @Data
    @Schema(title = "线路站点查询表单", description = "线路站点查询参数")
    public static class SysLineLocationFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "线路id")
        private String lineId;
        @Schema(description = "站点类型")
        private Integer type;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysLineLocation toSysLineLocation() {
            return new SysLineLocation(this);
        }
    }

    @Data
    @Schema(title = "线路站点保存表单", description = "线路站点保存参数")
    public static class SysLineLocationSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "线路id")
        private String lineId;
        @Schema(description = "站点类型")
        private Integer type;
        @Schema(description = "名称")
        private String name;

        public SysLineLocation toSysLineLocation() {
            return new SysLineLocation(this);
        }
    }
}
